
library(magrittr)
library(dplyr)
library(tidyverse)
library(poLCA)


# Load Data
df <- read.csv("Conspiracy Beliefs Cleaned.csv")



df <- df %>%
  mutate(ResponseID = row_number())

anyDuplicated(df$ResponseID)


##Match Behaviors to Theories##

theory_behaviors <- list(
  A2016Collusion = list(binary = "A2016Binary",
                        behaviors = c("DemBehav_1_1", "DemBehav_2_1", "DemBehav_3_1", "DemBehav_4_1")),
  A9_11 = list(binary = "A911Binary",
               behaviors = c("DemBehav_1_2", "DemBehav_2_2", "DemBehav_3_2", "DemBehav_4_2")),
  A2024Stolen = list(binary = "A2024Binary",
                     behaviors = c("DemBehav_1_3", "DemBehav_2_3", "DemBehav_3_3", "DemBehav_4_3")),
  DeepState = list(binary = "DeepStateBinary",
                   behaviors = c("NeutralBehav__1_1", "NeutralBehav__2_1", "NeutralBehav__3_1", "NeutralBehav__4_1")),
  UFOs = list(binary = "UFOBinary",
              behaviors = c("NeutralBehav__1_2", "NeutralBehav__2_2", "NeutralBehav__3_2", "NeutralBehav__4_2")),
  MoonLand = list(binary = "MoonlandBinary",
                  behaviors = c("NeutralBehav__1_3", "NeutralBehav__2_3", "NeutralBehav__3_3", "NeutralBehav__4_3")),
  Epstein = list(binary = "EpsteinBinary",
                 behaviors = c("NeutralBehav__1_4", "NeutralBehav__2_4", "NeutralBehav__3_4", "NeutralBehav__4_4")),
  SandyHook = list(binary = "SandyBinary",
                   behaviors = c("RepubBehav_1_1", "RepubBehav_2_1", "RepubBehav_3_1", "RepubBehav_4_1")),
  A2020Stolen = list(binary = "A2020Binary",
                     behaviors = c("RepubBehav_1_2", "RepubBehav_2_2", "RepubBehav_3_2", "RepubBehav_4_2")),
  QAnon = list(binary = "QAnonBinary",
               behaviors = c("RepubBehav_1_3", "RepubBehav_2_3", "RepubBehav_3_3", "RepubBehav_4_3"))
)



all_behaviors_long <- data.frame()

##Loop over each theory##
for (theory in names(theory_behaviors)) {
  
  binary_var <- theory_behaviors[[theory]]$binary
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  # Filter to believers 
  believers <- df[df[[binary_var]] == 1, behavior_vars]
  
  # Rename behavior columns 
  colnames(believers) <- c("Spoken", "Posted", "LikeMinded", "Verified")
  
  # Add to dataset
  all_behaviors_long <- bind_rows(all_behaviors_long, believers)
}

# Summarize average behaviors overall
overall_behavior <- all_behaviors_long %>%
  summarise(
    Avg_Spoken = mean(Spoken, na.rm = TRUE),
    Avg_Posted = mean(Posted, na.rm = TRUE),
    Avg_LikeMinded = mean(LikeMinded, na.rm = TRUE),
    Avg_Verified = mean(Verified, na.rm = TRUE),
    n = n()
  ) %>%
  mutate(across(starts_with("Avg_"), ~ round(.x, 2)))



# View!
print(overall_behavior)


##Of Belief Instances##
all_belief_instances <- data.frame()

for (theory in names(theory_behaviors)) {
  
  binary_var <- theory_behaviors[[theory]]$binary
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  # Filter to believers and select their behaviors
  believers <- df[df[[binary_var]] == 1, behavior_vars]
  
  # Standardize column names
  colnames(believers) <- c("Spoken", "Posted", "LikeMinded", "Verified")
  
  # Add theory label
  believers$Theory <- theory
  
  # Append
  all_belief_instances <- bind_rows(all_belief_instances, believers)
}



# Flag cases with no behaviors
belief_behavior_summary <- all_belief_instances %>%
  mutate(
    TotalBehaviors = rowSums(across(c(Spoken, Posted, LikeMinded, Verified)), na.rm = TRUE),
    NoBehavior = TotalBehaviors == 0
  ) %>%
  summarise(
    Total_Belief_Instances = n(),
    Num_NoBehavior = sum(NoBehavior),
    Percent_NoBehavior = round(100 * mean(NoBehavior), 2)
  )

# View result
print(belief_behavior_summary)


##Without Verified##
belief_behavior_summary <- all_belief_instances %>%
  mutate(
    TotalBehaviors = rowSums(across(c(Spoken, Posted, LikeMinded)), na.rm = TRUE),
    NoBehavior = TotalBehaviors == 0
  ) %>%
  summarise(
    Total_Belief_Instances = n(),
    Num_NoBehavior = sum(NoBehavior),
    Percent_NoBehavior = round(100 * mean(NoBehavior), 2)
  )

print(belief_behavior_summary)


## Calculate average engagement in each behavior type across belief instances ##
avg_behavior_per_type <- all_belief_instances %>%
  summarise(
    Avg_Spoken = mean(Spoken, na.rm = TRUE),
    Avg_Posted = mean(Posted, na.rm = TRUE),
    Avg_LikeMinded = mean(LikeMinded, na.rm = TRUE),
    Avg_Verified = mean(Verified, na.rm = TRUE),
    n = n()
  ) %>%
  mutate(across(starts_with("Avg_"), ~ round(.x, 2)))

# View result
print(avg_behavior_per_type)


##Never-Behaved Respondents##

library(dplyr)


respondent_behavior_flags <- dplyr::select(df, ResponseID)


for (theory in names(theory_behaviors)) {
  binary_var <- theory_behaviors[[theory]]$binary
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  
  respondent_behavior_flags[[paste0(theory, "_Belief")]] <- ifelse(
    df[[binary_var]] == 1, 1, 0
  )
  
  respondent_behavior_flags[[paste0(theory, "_AnyBehavior")]] <- ifelse(
    df[[binary_var]] == 1 & rowSums(df[, behavior_vars], na.rm = TRUE) > 0,
    1, 0
  )
}

##Summarize belief vs. behavior by respondent##
respondent_behavior_summary <- respondent_behavior_flags %>%
  rowwise() %>%
  mutate(
    BelievedAnything = sum(c_across(ends_with("_Belief")), na.rm = TRUE) > 0,
    DidAnyBehavior = sum(c_across(ends_with("_AnyBehavior")), na.rm = TRUE) >= 1
  ) %>%
  ungroup()

# Summary table
belief_without_behavior_summary <- respondent_behavior_summary %>%
  summarise(
    Total_Respondents = n(),
    Believed_Something = sum(BelievedAnything),
    Never_Acted = sum(BelievedAnything & !DidAnyBehavior),
    Pct_Never_Acted = round(100 * mean(BelievedAnything & !DidAnyBehavior), 2)
  )

# Print result
print(belief_without_behavior_summary)



table(respondent_behavior_summary$BelievedAnything, respondent_behavior_summary$DidAnyBehavior)






##By Certainty##
certainty_behavior_data <- data.frame()

for (theory in names(theory_behaviors)) {
  
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  for (level in c(4, 5)) {
    
    subset <- df %>%
      filter(!!sym(theory) == level)
    
    if (nrow(subset) > 0) {
      subset$TotalBehavior <- rowSums(subset[, behavior_vars], na.rm = TRUE)
      percent_no_behavior <- round(mean(subset$TotalBehavior == 0, na.rm = TRUE) * 100, 2)
      
      certainty_behavior_data <- rbind(certainty_behavior_data, data.frame(
        Theory = theory,
        CertaintyLevel = level,
        PercentNoBehavior = percent_no_behavior,
        n = nrow(subset)
      ))
    }
  }
}

# View by theory
print(certainty_behavior_data)

# Get average across theories
avg_by_certainty <- certainty_behavior_data %>%
  group_by(CertaintyLevel) %>%
  summarise(AvgPercentNoBehavior = round(mean(PercentNoBehavior), 2))

print(avg_by_certainty)


# Subset to belief instances with certainty = 4 or 5
library(dplyr)

all_instances <- data.frame()

for (theory in names(theory_behaviors)) {
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  for (level in c(4, 5)) {
    
    subset <- df %>%
      filter(!!sym(theory) == level)
    
    if (nrow(subset) > 0) {
      behavior_sum <- rowSums(subset[, behavior_vars], na.rm = TRUE)
      
      subset$Certainty <- level
      subset$Theory <- theory
      subset$NoBehavior <- behavior_sum == 0
      
      all_instances <- bind_rows(all_instances, subset)
    }
  }
}

tab <- table(all_instances$Certainty, all_instances$NoBehavior)

prop.test(
  x = c(tab["4", "TRUE"], tab["5", "TRUE"]),
  n = c(sum(tab["4", ]), sum(tab["5", ]))
)


##Number of behaviors by Certainty##

library(dplyr)


all_instances <- data.frame()

for (theory in names(theory_behaviors)) {
  behavior_vars <- theory_behaviors[[theory]]$behaviors
  
  for (level in c(4, 5)) {
    subset <- df %>%
      filter(!!sym(theory) == level)
    
    if (nrow(subset) > 0) {
      behavior_sum <- rowSums(subset[, behavior_vars], na.rm = TRUE)
      
      subset <- subset %>%
        mutate(
          Certainty = level,
          Theory = theory,
          TotalBehavior = behavior_sum
        )
      
      all_instances <- bind_rows(all_instances, subset)
    }
  }
}

## Get mean number of behaviors by certainty level ##
avg_behaviors_by_certainty <- all_instances %>%
  group_by(Certainty) %>%
  summarise(
    Avg_Behaviors = round(mean(TotalBehavior, na.rm = TRUE), 2),
    n = n()
  )

print(avg_behaviors_by_certainty)





